package bcMedTests;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import bcMedBeans.Consulta;
import bcMedBeans.Endereco;
import bcMedBeans.Medico;
import bcMedBeans.Paciente;
import bcMedBeans.PlanoSaude;
import bcMedBeans.StatusConsulta;
import bcMedBeans.TipoConsulta;
import bcMedEntity.EntityDataBase;
import bcMedEntity.EntityDataBaseRead;
import bcMedEntity.EntityDataBaseSearch;
import bcMedEntity.EntityDataBaseWrite;

public class TestEntityDataBaseWrite {
	EntityDataBase testeDb;
	
	@BeforeClass
	public void setUp() {
		testeDb = new EntityDataBase();
	}
	
	@Test
	public void testDrivePresente() {
		assert testeDb.isDriverPresent();
	}
	
	
	
	@Test
	public void testWriteEndereco() {
	
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		
		List<Endereco> enderecoList = new ArrayList<Endereco>();
		Endereco end = new Endereco();
		
		end.setBairro("bTest");
		end.setCep("bTest");
		end.setCidade("bTest");
		end.setEstado("bTest");
		end.setNumero(7);
		end.setRua("bTest");
		
		dbWrite.writeEndereco(end);
		
		enderecoList= dbSearch.getEnderecoRestricted("bairro = 'bTest'");
		
		boolean c1 = enderecoList.get(0).getBairro().equals("bTest");
		boolean c2 = enderecoList.get(0).getCep().equals("bTest");
		boolean c3 = enderecoList.get(0).getCidade().equals("bTest");
		boolean c4 = enderecoList.get(0).getNumero() == 7;
		boolean c5 = enderecoList.get(0).getEstado().equals("bTest");
		assert (c1 && c2 && c3 && c4 && c5);
		
		
		
		
		
	}
	
	
	@Test
	public void testWritePlanoSaude() {
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		PlanoSaude p = new PlanoSaude();
		List<PlanoSaude> l = new ArrayList<PlanoSaude>();
		
		p.setNome("planoTest");
		dbWrite.writePlanoSaude(p);
		
		l = dbSearch.getPlanoSaudeRestricted("nome = 'planoTest'");
		
		assert !l.isEmpty();
		
	}
	
	
	
	@Test
	public void testWriteStatusConsulta() {
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		
		StatusConsulta sc = new StatusConsulta();
		List<StatusConsulta> sc1 = new ArrayList<StatusConsulta>();
		
		sc.setNome("teste01");
		dbWrite.writeStatusConsulta(sc);
		
		sc1 = dbSearch.getStatusConsultaRestricted("nome = 'teste01'");
		assert sc1.isEmpty() == false;
	}
	
	
	@Test
	public void testWriteConsulta(){
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);		
		EntityDataBaseRead dbRead = new EntityDataBaseRead(testeDb);
		Consulta c = new Consulta();
		Date c_date = new Date();
		Medico c_medico = dbRead.readMedico(1);
		Paciente c_paciente = dbRead.readPaciente(1);
		StatusConsulta c_status = dbRead.readStatusConsulta(1);
		TipoConsulta c_tipo = dbRead.readTipoConsulta(1);
		if(c_medico == null) System.out.println("c_medico == null");
		if(c_paciente == null) System.out.println("c_paciente == null");
		if(c_status == null) System.out.println("c_status == null");
		if(c_tipo == null) System.out.println("c_tipo == null");
		
		c.setData(c_date);
		c.setMedico(c_medico);
		c.setPaciente(c_paciente);
		c.setStatusConsulta(c_status);
		c.setTipoConsulta(c_tipo);
		
		int size_before = dbSearch.getConsultaAll().size();
		dbWrite.writeConsulta(c);
		int size_after = dbSearch.getConsultaAll().size();			
		assert (size_before+1 == size_after);
	}

	@Test
	public void testWriteMedico() {
		
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		Medico m = new Medico();
		m.setNome("carlosbentos");
		dbWrite.writeMedico(m);
		List<Medico> medicoList= dbSearch.getMedicoRestricted("nome = 'carlosbentos'");
		assert !medicoList.isEmpty();
		
	}
	
	
	@Test
	public void testWritePaciente(){
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		EntityDataBaseRead dbRead = new EntityDataBaseRead(testeDb);
		Paciente p = new Paciente();
		Date p_date = new Date();
		Endereco p_endereco = dbRead.readEndereco(1);
		PlanoSaude p_plano = dbRead.readPlanoSaude(1);
		p.setCadastro(p_date);
		p.setEndereco(p_endereco);
		p.setIdade(20);
		p.setNome("TesteJon");
		p.setPlanoSaude(p_plano);
		p.setSexo('m');
		
		dbWrite.writePaciente(p);
		
		List<Paciente> p_list = dbSearch.getPacienteRestricted("nome = 'TesteJon'");
		assert p_list.isEmpty() == false;
	}
	
	
	@Test
	public void testWriteTipoConsutla(){
		testeDb.connectDataBase();
		EntityDataBaseWrite dbWrite = new EntityDataBaseWrite(testeDb);
		EntityDataBaseSearch dbSearch = new EntityDataBaseSearch(testeDb);
		TipoConsulta tc = new TipoConsulta();
		tc.setNome("ProcedimentoA");
		dbWrite.writeTipoConsutla(tc);
		List<TipoConsulta> tc_list = dbSearch.getTipoConsultaRestricted("nome = 'ProcedimentoA'");
		assert tc_list.isEmpty() == false;
	}
	
	
}
